package com.example.showlocation;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class RequestPermissionWithLauncherActivity extends AppCompatActivity {
    ActivityResultLauncher<String> requestPermissionLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_permission_with_launcher);

        requestPermissionLauncher =
                registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                    if (isGranted) {
                        Log.d(getLocalClassName()+":","isGranted" );
                        // Permission is granted. Continue the action or workflow in your
                        // app.
                    } else {
                        Log.d(getLocalClassName()+":","else" );
                        //requestPermissionLauncher.launch("android.permission.ACCESS_FINE_LOCATION");
                        // Explain to the user that the feature is unavailable because the
                        // feature requires a permission that the user has denied. At the
                        // same time, respect the user's decision. Don't link to system
                        // settings in an effort to convince the user to change their
                        // decision.
                    }
                });
    }
    @SuppressLint("IntentReset")
    public void RequestPermission(View view) throws Exception {
        if (ContextCompat.checkSelfPermission(
                getApplicationContext(), "android.permission.ACCESS_FINE_LOCATION") ==
                PackageManager.PERMISSION_GRANTED) {
            // You can use the API that requires the permission.
        } else if (ActivityCompat.shouldShowRequestPermissionRationale(
                this, "android.permission.ACCESS_FINE_LOCATION")) {
            // In an educational UI, explain to the user why your app requires this
            // permission for a specific feature to behave as expected, and what
            // features are disabled if it's declined. In this UI, include a
            // "cancel" or "no thanks" button that lets the user continue
            // using your app without granting the permission.
        } else {
            requestPermissionLauncher.launch("android.permission.ACCESS_FINE_LOCATION");
            // You can directly ask for the permission.
            Log.d(getLocalClassName()+":", "else");
        }
        Log.d(getLocalClassName()+":", "RequestPermission");
    }
}
 /*ActivityResultContract activityResultContract=new ActivityResultContract() {
            @NonNull
            @Override
            public Intent createIntent(@NonNull Context context, Object o) {
                return null;
            }

            @Override
            public Object parseResult(int i, @Nullable Intent intent) {
                return null;
            }
        };*/
 /*ActivityResultCallback activityResultCallback=new ActivityResultCallback() {
        @Override
        public void onActivityResult(Object o) {

            Log.d(getLocalClassName()+":", o.toString());
        }
    };*/